package id.pratama.pantaijogja.util;

import id.pratama.pantaijogja.R;
import id.pratama.pantaijogja.entity.Pantai;

import java.util.List;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter
{

	// class untuk holding item di customlistview
	static class ViewHolder
	{
		ImageView	gambar;
		TextView	txtNama;
		TextView	txtAlamat;
	}

	private ImageLoader			mImageLoader;
	private DisplayImageOptions	mOptions;

	private List<Pantai>		mListPantai;
	private LayoutInflater		mInflater;

	public CustomAdapter(Context context, ImageLoader imageLoader,
			DisplayImageOptions options, List<Pantai> listPantai)
	{
		mInflater = LayoutInflater.from(context);
		this.mImageLoader = imageLoader;
		this.mOptions = options;
		this.mListPantai = listPantai;
	}

	@Override
	public int getCount()
	{
		// TODO Auto-generated method stub
		return mListPantai.size();
	}

	@Override
	public Object getItem(int arg0)
	{
		// TODO Auto-generated method stub
		return mListPantai.get(arg0);
	}

	@Override
	public long getItemId(int arg0)
	{
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder holder;

		// cek apakah convertview masih kosong
		if (convertView == null)
		{
			// jika iya maka inisialisasi item2 di single custom listview
			convertView = mInflater.inflate(R.layout.single_item, null);

			holder = new ViewHolder();
			holder.gambar = (ImageView) convertView
					.findViewById(R.id.thumbnail);
			holder.txtNama = (TextView) convertView.findViewById(R.id.txt_nama);
			holder.txtAlamat = (TextView) convertView
					.findViewById(R.id.txt_alamat);

			// set tag convertview
			convertView.setTag(holder);
		} else
		{
			// jika tidak kosong maka ambil tag
			holder = (ViewHolder) convertView.getTag();
		}

		// set resource nama, alamat, dan gambar,
		// untuk gambar menggunakan imageloader
		holder.txtNama.setText(mListPantai.get(position).getNama());
		holder.txtAlamat.setText(mListPantai.get(position).getAlamat());
		mImageLoader.displayImage(mListPantai.get(position).getGambar(),
				holder.gambar, mOptions);

		return convertView;
	}

}
